package com.cryptooj.project.model.vo;

import cn.hutool.json.JSONUtil;
import com.cryptooj.project.model.dto.question.JudgeConfig;
import com.cryptooj.project.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 *  题目脱敏VO
 * */
@Data
public class QuestionVO implements Serializable {

    private Long id;

    private String title;

    private String content;

    private List<String> tags;

    private Integer submitNum;

    private Integer acceptNum;

    private JudgeConfig judgeConfig;

    private Integer thumbNum;

    private Integer favourNum;

    private Date createTime;

    private Date updateTime;

    private UserVO userVO;

    public static final Long serialVersion = 1L;

    /**
     *  VO包装类转对象
     * */
    public static Question voToObj(QuestionVO questionVO){
        if(questionVO == null){
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionVO,question);
        List<String> tags = questionVO.getTags();
        if(tags != null){
            question.setTags(JSONUtil.toJsonStr(tags));
        }
        JudgeConfig judgeConfig1 = questionVO.getJudgeConfig();
        if(judgeConfig1!=null){
            question.setJudgeConfig(JSONUtil.toJsonStr(judgeConfig1));
        }
        return question;
    }

    /**
     *  对象转VO包装类
     * */
    public static QuestionVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionVO questionVO = new QuestionVO();
        BeanUtils.copyProperties(question, questionVO);
        String tags = question.getTags();
        List<String> tagList = JSONUtil.toList(tags, String.class);
        questionVO.setTags(tagList);
        String judgeConfig = question.getJudgeConfig();
        questionVO.setJudgeConfig(JSONUtil.toBean(judgeConfig,JudgeConfig.class));
        return questionVO;
    }
}
